package com.dkd.manage.controller;

import com.dkd.common.core.domain.R;
import com.dkd.manage.pojo.dto.VmAddDto;
import com.dkd.manage.pojo.dto.VmQueryListDto;
import com.dkd.manage.pojo.entity.VendingMachine;
import com.dkd.manage.pojo.vo.VendingMachineVo;
import com.dkd.manage.result.PageResult;
import com.dkd.manage.service.VmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "设备管理相关接口")
@RequestMapping("/manage/vm")
public class VmController {

    @Autowired
    private VmService vmService;

    @ApiOperation("分页查询设备列表")
    @PreAuthorize("@ss.hasPermi('manage:vm:list')")
    @GetMapping("/list")
    public R<PageResult<VendingMachineVo>> list(VmQueryListDto vmQueryListDto) {
        log.info("分页查询设备列表：{}", vmQueryListDto);
        PageResult<VendingMachineVo> result = vmService.list(vmQueryListDto);
        return R.ok(result);
    }

    @ApiOperation("新增设备")
    @PreAuthorize("@ss.hasPermi('manage:vm:add')")
    @PostMapping
    public R<Integer> add(@RequestBody VmAddDto vmAddDto) {
        log.info("新增设备：{}", vmAddDto);
        Integer rows = vmService.add(vmAddDto);
        return R.ok(rows);
    }

    @ApiOperation("根据id查询设备")
    @PreAuthorize("@ss.hasPermi('manage:vm:query')")
    @GetMapping("/{id}")
    public R<VendingMachine> getVmById(@PathVariable Long id) {
        log.info("根据id查询设备：{}", id);
        VendingMachine vendingMachine = vmService.getVmById(id);
        return R.ok(vendingMachine);
    }

    @ApiOperation("修改设备")
    @PreAuthorize("@ss.hasPermi('manage:vm:edit')")
    @PutMapping
    public R<Integer> edit(@RequestBody VendingMachine vendingMachine) {
        log.info("修改设备：{}", vendingMachine);
        Integer rows = vmService.edit(vendingMachine);
        return R.ok(rows);
    }

    @ApiOperation("根据id删除设备")
    @PreAuthorize("@ss.hasPermi('manage:vm:remove')")
    @DeleteMapping("/{ids}")
    public R<Integer> deleteVmById(@PathVariable Long[] ids) {
        log.info("根据id删除设备：{}", ids);
        Integer rows = vmService.deleteVmById(ids);
        return R.ok(rows);
    }
}
